<?php

//UMA new
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class InputOption extends Model
{
    protected $fillable = [
        'input_item_id',
        'option_name',
        'option',
        'rate',
    ];

    /**
     * 每个 option 属于一个item
     * すべてのオプションはアイテムに属します
     * @return belongsTo
     */
    public function item()
    {
        return $this->belongsTo(InputItem::class, 'input_item_id');
    }

    /**
     *
     * @param  mixed $query
     * @param  mixed $pid
     * @return query
     */
    public function scopeByItemId($query, $pid)
    {
        return $query->where('input_item_id', $pid);
    }

    /**
     * query scope
     *
     * @param  Builder $query
     * @param  mixed $option
     * @return query
     */
    public function scopeLessThanOption($query, $option)
    {
        if (empty($option)) {
            return $query->where('option', 0);
        }
        return $query->where('option', '<=', $option);
    }
}
